﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO.IsolatedStorage;
using System.Diagnostics;
using System.Collections.Generic;
using Microsoft.Phone.Shell;

namespace WPKeyboard
{
    public class AppSettings
    {
        private const string constPlaySound = "PlaySound";
        private const string constVibrate = "Vibrate";
        private const string constEnglishKeyboardChecked = "EnglishKeyboardChecked";

        public const string constKeyboardChecked = "KeyboardChecked";
        
        // Our isolated storage settings
        IsolatedStorageSettings isolatedStore;

        /// <summary>
        /// Constructor that gets the application settings.
        /// </summary>
        public AppSettings()
        {
            try
            {
                // Get the settings for this application.
                isolatedStore = IsolatedStorageSettings.ApplicationSettings;

                // Copy the current settings into the text boxes as the new values
                // entered in will not be saved until the user clicks the 'done' button.
                //textBoxUsername.Text = settings.UsernameSetting;
                //passwordBoxPassword.Password = settings.PasswordSetting;
            }
            catch (Exception e)
            {
                Debug.WriteLine("Exception while using IsolatedStorageSettings: " + e.ToString());
            }
        }

        public bool IsEnglishKeyboardChecked { 
            get {
                return GetValueOrDefault(constEnglishKeyboardChecked, true);
            }
            set {
                AddOrUpdateValue(constEnglishKeyboardChecked, value);
            }
        }

        public bool IsPlayClickSoundChecked { 
            get{
                // Loading the PlaySound settings to the page
                return GetValueOrDefault<bool>(constPlaySound, true);
            }
            set{
                AddOrUpdateValue(constPlaySound, value);           
            }
        }
                        
        public bool IsVibrationChecked { 
            get {
            
                // Loading the Vibrate settings to the page
                return GetValueOrDefault<bool>(constVibrate, false);
            } 
            set{
                AddOrUpdateValue(constVibrate,value);
            } 
        }

        public bool GetOtherKeyboardChecked(string pKeyboardKey)
        {
            return GetValueOrDefault<bool>(pKeyboardKey, false);
        }

        public void SetOtherKeyboardChecked(string pKeyboardKey, bool pValue)
        {
            AddOrUpdateValue(pKeyboardKey, pValue);
        }


        /// <summary>
        /// Checks if the key exists in the store
        /// if exists, return true, else false.
        /// </summary>
        /// <param name="Key"></param>
        /// <returns></returns>
        private bool IsKeyExist(string Key)
        {
            return isolatedStore.Contains(Key);
        }


        /// <summary>
        /// Update a setting value for our application. If the setting does not
        /// exist, then add the setting.
        /// </summary>
        /// <param name="Key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        private bool AddOrUpdateValue(string Key, Object value)
        {
            bool valueChanged = false;

            try
            {
                // if new value is different, set the new value.
                if (isolatedStore[Key] != value)
                {
                    isolatedStore[Key] = value;
                    valueChanged = true;
                }
            }
            catch (KeyNotFoundException)
            {
                isolatedStore.Add(Key, value);
                valueChanged = true;
            }
            catch (ArgumentException)
            {
                isolatedStore.Add(Key, value);
                valueChanged = true;
            }
            catch (Exception e)
            {
                Debug.WriteLine("Exception while using IsolatedStorageSettings: " + e.ToString());
            }

            return valueChanged;
        }


        /// <summary>
        /// Get the current value of the setting, or if it is not found, set the 
        /// setting to the default setting.
        /// </summary>
        /// <typeparam name="valueType"></typeparam>
        /// <param name="Key"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        private valueType GetValueOrDefault<valueType>(string Key, valueType defaultValue)
        {
            valueType value;

            try
            {
                value = (valueType)isolatedStore[Key];
            }
            catch (KeyNotFoundException)
            {
                value = defaultValue;
            }
            catch (ArgumentException)
            {
                value = defaultValue;
            }

            return value;
        }


        /// <summary>
        /// Save the settings.
        /// </summary>
        public void Save()
        {
            isolatedStore.Save();
        }


        public void LoadSettings()
        {
            bool blnIsDirty = false;

            // Checks if there are settings saves in the IsolatedStorage, if not, sets the default settings
            if (IsKeyExist(constEnglishKeyboardChecked) == false)
            {
                IsEnglishKeyboardChecked = true;
                
                blnIsDirty = true;
            }

            if (IsKeyExist(constPlaySound) == false)
            {
                IsPlayClickSoundChecked = true;

                blnIsDirty = true;
            }

            if (blnIsDirty)
	        {
                Save();
	        }  
        }
    }
}
